package com.example.attendancesystem_1_8.backendInterface;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.net.URLEncoder;

public class Account
{

    private static final String URL="http://47.96.144.238:8080/user";


    /**
     * 登录
     * @param account 账号
     * @param password 密码
     */
    public static int login(String account,String password)
    {
        JSONObject json=new JSONObject();
        json.put("account",account);
        json.put("password",password);

        JSONObject ans= JSON.parseObject(HttpRequest.sendPost(URL+"/verifyPassword",json.toString()));

        if(ans.getString("success").equals("true"))
        {
            System.out.println("账号密码正确");
            System.out.println("该用户账号为:"+ans.getString("id"));
            System.out.println("该用户用户名为:"+ans.getString("userName"));
            System.out.println("该用户的身份是:"+ans.getString("userType"));
            System.out.println("该用户的上级id)"+ans.getString("leaderId"));
            return 0;
        }
        else
        {
            System.out.println("登录失败");
            switch(ans.getString("error"))
            {
                case "1":
                    System.out.println("账号不存在");
                    return 1;
                case "2":
                    System.out.println("密码错误");
                    return 2;
                case "3":
                    System.out.println("账号或密码为空");
                    return 3;
            }
        }
        return 3;
    }


    /**
     * 注册
     * @param account 账号
     * @param password 密码
     * @param userName 用户名
     * @param worker_type 员工类型
     */
    public static int register(String account,String password,String userName,int worker_type)
    {
        String url=URL+"/addUser";
        JSONObject json=new JSONObject();
        json.put("account",account);
        json.put("password",password);
        json.put("username",userName);
        json.put("type",worker_type);
        //Log.i("tag",url);
        JSONObject ans=JSONObject.parseObject(HttpRequest.sendPost(url,json.toString()));

        if(ans.getString("success").equals("true"))
        {
            System.out.println("注册成功");
            login(account,password);
            return 0;
        }
        else
        {
            switch(ans.getString("error"))
            {
                case "1":
                    System.out.println("账号或密码长度有问题");
                    return 1;
                case "2":
                    System.out.println("账号已存在，注册失败");
                    return 2;
                case "":
                    System.out.println("数据库内部问题导致注册失败");
                    return 3;
            }
        }
        return 3;
    }


    /**
     * 设置用户的人脸信息
     * @param account 账号
     * @param password 密码
     * @param picture 待上传的照片
     */
    public static int setPicture(String account, String password, File picture) throws Exception
    {
        String url=URL+"/addFaceInfo";

        String pictureStr=PhotoBase.photoToString(picture.getPath());
        pictureStr= URLEncoder.encode(pictureStr,"UTF-8");


        JSONObject json=new JSONObject();
        json.put("account",account);
        json.put("password",password);
        json.put("faceInfo",pictureStr);

        JSONObject ans=JSONObject.parseObject(HttpRequest.sendPost(url,json.toString()));

        if(ans.getString("success").equals("true"))
        {
            System.out.println("设置成功");
            return 0;
        }
        else
        {
            switch(ans.getString("error"))
            {
                case "1":
                    System.out.println("服务器可能坏了");
                    return 1;

                case "2":
                    System.out.println("密码可能8对");
                    return 2;

                case "3":
                    System.out.println("照片里可能人数不是1");
                    return 3;

            }
        }
        return 4;
    }


    /**
     * 获取一张照片与数据库中已存照片的相似度
     * @param account 账号
     * @param password 密码
     * @param picture 待上传的照片
     */
    public static int getSimilar(String account, String password, File picture) throws Exception
    {
        String url=URL+"/getSimilar";

        String pictureStr=PhotoBase.photoToString(picture.getPath());
        pictureStr= URLEncoder.encode(pictureStr,"UTF-8");


        JSONObject json=new JSONObject();
        json.put("account",account);
        json.put("password",password);
        json.put("faceInfo",pictureStr);

        JSONObject ans=JSONObject.parseObject(HttpRequest.sendPost(url,json.toString()));

        if(ans.getString("success").equals("true"))
        {
            System.out.println("获取成功");
            System.out.println("两张照片的相似度为:"+ans.getFloat("similar"));
            return 0;
        }
        else
        {
            switch(ans.getString("error"))
            {
                case "1":
                    System.out.println("服务器可能坏了(或者是你之前没上传本人照片");
                    return 1;
                case "2":
                    System.out.println("密码可能8对");
                    return 2;
                case "3":
                    System.out.println("照片里可能人数不是1");
                    return 3;
            }
        }
        return 4;
    }
    /*
    检查某用户是否已上传人脸信息
     */
    public static int TextFace(String account, String password) throws Exception
    {
        String url=URL+"/FaceExist";




        JSONObject json=new JSONObject();
        json.put("account",account);
        json.put("password",password);


        JSONObject ans=JSONObject.parseObject(HttpRequest.sendPost(url,json.toString()));

        if(ans.getString("success").equals("true"))
        {
            System.out.println("照片已上传");

            return 0;
        }
        else
        {
            System.out.println("照片未上传");

            return 1;
        }

    }









    public static void main(String[] args) throws Exception
    {



        //login("123456","654321");
        getSimilar("123456","654321",new File("D:/firefox/faceimg/a1.jpg"));


    }





}
